package cn.edu.zut.controller;

import cn.edu.zut.domain.Message;
import cn.edu.zut.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService service;

    @RequestMapping("/add.do")
    public @ResponseBody void add(String userId, String title, String content, String time, String vis){
        int _id = service.findEmptyMessageId();//找一个通知表中没有的_id
        Message message = new Message(_id, userId, title, content, time, vis);
        service.add(message);
    }

    @RequestMapping("/delete.do")
    public @ResponseBody void deleteById(int _id){
        service.deleteById(_id);
    }

    @RequestMapping("/findByUserId.do")
    public @ResponseBody List<Message> findByUserId(String userId){
        return service.findByUserId(userId);
    }

    @RequestMapping("/findAllNoVis.do")
    public @ResponseBody List<Message> findAllNoVis(String userId){
        List<Message> list = service.findAllNoVis(userId,"0");
        for(int i=0;i<list.size();i++){//将查找到的这些信息标记，表示发送过了
            Message message = list.get(i);
            message.setVis("1");
            service.update(message);
        }
        return list;
    }
    @RequestMapping("/findById.do")
    public @ResponseBody Message findById(int _id){
        return service.findById(_id);
    }
}
